package com.easysoft.framework;


import com.easysoft.framework.manager.model.SysManager;
import com.easysoft.framework.manager.service.SysManagerService;
import com.easysoft.framework.role.model.SysRoleView;
import com.easysoft.framework.role.service.RoleService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

public class CustomAdminRealm extends AuthorizingRealm {

    @Resource
    @Qualifier("service.SysManagerService")
    private SysManagerService managerService;

    @Resource
    @Qualifier("service.SysRoleService")
    private RoleService roleService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        String name = (String) principals.getPrimaryPrincipal();
        SysManager manager = managerService.selectManagerByUserName(name);
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        List<SysRoleView> userRoles = roleService.selectSysRoleByUserId(manager.getId());
        List<String> roles = new ArrayList<>();
        for (SysRoleView roleView : userRoles){
            roles.add(roleView.getRoleName());
        }
        info.addRoles(roles);
        return info;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String name = (String) token.getPrincipal();
        SysManager manager = managerService.selectManagerByUserName(name);
        if (manager == null) {
            throw new UnknownAccountException();
        }
        String pwd = new String((char[]) token.getCredentials());
        String rePwd = DigestUtils.md5DigestAsHex((pwd + manager.getSalt()).getBytes());
        if (!rePwd.equals(manager.getPassword())) {
            throw new IncorrectCredentialsException();
        }
        return new SimpleAuthenticationInfo(name, pwd, getName());
    }

 /*   @Resource
    private UserService UserService;

    @Resource
    private SysRoleService sysRoleService;

    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        SysUser user = (SysUser) principals.getPrimaryPrincipal();
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        List<SysRole> roleList = sysUserService.getRoles(user.getId());
        List<String> roles = new ArrayList<>();
        Set<String> permissions = new HashSet<>();
        for (SysRole role : roleList){
            roles.add(role.getName());
            permissions.addAll(sysRoleService.getPermission(role.getId()));
        }
        info.addRoles(roles);
        info.addStringPermissions(permissions);
        return info;
    }*/

}
